// version 1
var parseModule = function (baseUrl) {
	var wholePath = parseUri(document.URL).path;
	var actualPath = wholePath.substring(baseUrl.length);
	var pathPieces = actualPath.split('/');
	var controllerArray= new Array('admin','api');
	var arLen=controllerArray.length;
	var i=0;
	while(i < arLen){
		if(pathPieces[1].toLowerCase() == controllerArray[i].toLowerCase()) {
			return(pathPieces[2]);
		};
		i++;
	};
	switch(pathPieces[1])
	{
	case 'my':
	case 'default':
	case 'tag':
		return('');
		break;
	default:
		return(pathPieces[1]);
	}
};

var resetDom = function (userCookie) {
if(userCookie.get('level')=='A') {
	var baseUrl = URLDecode(userCookie.get('baseUrl'));
	var userName = decodeURIComponent(userCookie.get('name'));
	var moduleName = parseModule(baseUrl);
	var txt = document.createTextNode('Admin');
	var menu1 = document.createElement('a');
	menu1.href=baseUrl + '/admin/'+moduleName;
	menu1.appendChild(txt);
	var menu_item = document.createElement('li');
	menu_item.appendChild(menu1);
	menu_item.className = 'page_item';
	document.getElementById('topMenu').appendChild(menu_item);
}
	var loginBlock = document.getElementById('user_blockToggle');
	loginBlock.innerHTML="<h4>Status</h4>Hi "+userName+"<br />"
	+'<a href="'+baseUrl+'/user/logout/">Logout</a>';

};

var URLDecode = function (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


userCookie  = new cookieObject("u",365,"/","ver","name","level","mode","baseUrl");
/*
userCookie.put ('name',  'alvin')
userCookie.put ('level',  'a')
userCookie.put ('mode',  'test')
userCookie.put ('baseUrl', '/zend/veneer2')
userCookie.write()
*/
if(userCookie.found) {
	result = resetDom(userCookie);
}
